package com.webex.ta.hydra.hamal.action;

import com.thoughtworks.xstream.XStream;
import com.webex.ta.hydra.core.AbstractAction;
import com.webex.ta.hydra.core.ParamDescriptor;
import com.webex.ta.hydra.core.TestResult;
import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.hamal.Connections;
import com.webex.ta.hydra.hamal.iface.CommandResult;
import com.webex.ta.hydra.hamal.iface.Hamal;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-6
 * Time: 14:24:00
 */
public class HamalExecute extends AbstractAction {
    private static XStream xs = new XStream();

    private static ParamDescriptor[] desc = new ParamDescriptor[4];
    static {
        desc[0] = new ParamDescriptor("connection name");
        desc[1] = new ParamDescriptor("Hamal action name");
        desc[2] = new ParamDescriptor("Hamal target");
        desc[3] = new ParamDescriptor("Hamal value", true);
    }

    public void execute(TestResult result, String... args) throws Exception {
        Hamal.Client client = Connections.getInstance().getConnection(args[0].trim());
        if (client == null)
            throw new Exception("connection " + args[0] + " not found");
        CommandResult r = client.execute(args[1], args[2], args[3]);
        if (r.getStatuscode() != 0)
            result.setFailure(true);

        result.setResponse(xs.toXML(r));
    }

    public ParamDescriptor[] getParamDescriptors(Command cmd) {
        return desc;
    }
}
